var WastageMachineInfo = function (wastage) {
  BaseModel.call(this);

  var self = this;
  var fn = {
    initData: function (wastage) {
      var fields = ['equipmentNo', 'equipmentImage', 'deptId', 'deptName', 'workShopId', 'workShopName'];
      self.setDataFromObj(wastage, fields);

      self.todayCount = null;
      var records = wastage.equipmentRecordPojos;

      var recordObj = {};
      var maxPerEletic = 0;
      var beforeMonthCount = 0,
        monthUseEletic = 0,
        todayUseEletic = 0;
      for (var i = 0; i < records.length; i++) {
        var record = records[i];
        var createTime = record.createTime;
        if (SysUtil.isEmpty(createTime)) {
          createTime = moment().startOf('month').format('YYYY-MM-DD');
          record.createTime = createTime;
        }

        var machingCount = record.machingCount;
        if (machingCount < 0) {
          machingCount = 0;
          record.machingCount = 0;
        }
        if (moment().isSame(createTime, 'month')) {
          monthUseEletic += record.useEletic;
          if (moment().isSame(createTime, 'day')) {
            todayUseEletic = record.useEletic;
          } else {
            beforeMonthCount += machingCount;
          }
        }

        var useEletic = record.useEletic;
        useEletic = useEletic * 1000;
        var perEletic = this.getPerEletic(useEletic, machingCount);
        if (maxPerEletic < perEletic) {
          maxPerEletic = perEletic;
        }

        record.perEletic = perEletic;
        recordObj[createTime] = record;
      }

      self.todayUseEletic = todayUseEletic;
      self.monthUseEletic = monthUseEletic;
      self.beforeMonthCount = beforeMonthCount;
      self.maxBeforePerEletic = maxPerEletic;
      self.recordObj = recordObj;
      self.recordData = null;
    },

    getPerEletic: function (useEletic, count) {
      if (count === 0) {
        return 0;
      }
      var perEletic = useEletic / count;
      perEletic = _.round(perEletic, 2);
      return perEletic;
    },

    setRecordData: function () {
      var dayMoment = moment().subtract(29, 'day');
      var endMoment = moment();
      var recordObj = self.recordObj;
      var recordData = [];
      while (dayMoment.isBefore(endMoment, 'day')) {
        var day = dayMoment.format('YYYY-MM-DD');
        var record = recordObj[day];
        if (record === undefined) {
          record = {
            createTime: day,
            machingCount: 0,
            useEletic: 0,
            perEletic: 0
          };
        }
        recordData.push(record);
        dayMoment = dayMoment.add(1, 'day');
      }
      self.recordData = recordData;
    }
  };

  this.resetRecord = function (data) {
    if (data === null) {
      this.todayCount = 0;
      this.monthCount = this.beforeMonthCount;
      this.todayPerEletic = 0;
      this.maxPerEletic = this.maxBeforePerEletic;
      return;
    }

    var todayCount = data.count;
    if (SysUtil.isEmpty(todayCount)) {
      todayCount = 0;
    } else {
      var startProportion = data.startProportion;
      if (SysUtil.isEmpty(startProportion)) {
        startProportion = 1;
      }
      var endProportion = data.endProportion;
      if (SysUtil.isEmpty(endProportion)) {
        endProportion = 1;
      }
      todayCount = Math.ceil(_.round(todayCount * startProportion / endProportion, 2));
    }

    var todayPerEletic = fn.getPerEletic(this.todayUseEletic, todayCount);

    this.todayCount = todayCount;
    this.todayPerEletic = todayPerEletic;
    this.monthCount = this.beforeMonthCount + todayCount;
    this.maxPerEletic = Math.max(this.maxBeforePerEletic, todayPerEletic);
  };

  this.getRecords = function (dateIndex) {
    if (this.recordData === null) {
      fn.setRecordData();
    }

    var recordData = this.recordData;
    recordData = recordData.slice(dateIndex, dateIndex + wastageBoardDatePageSize);
    recordData = _.clone(recordData);
    if (recordData.length < wastageBoardDatePageSize) {
      recordData.push({
        createTime: moment().format('YYYY-MM-DD'),
        machingCount: this.todayCount,
        useEletic: this.todayUseEletic,
        perEletic: this.todayPerEletic
      });
    }
    return recordData;
  };

  fn.initData(wastage);
};
